<script type="text/javascript">
var $datatable;
var _DATATABLE_TR;
var oTable;
    
$(document).ready(function() {
    $datatable = $('#datatable_dvt');
    $dialog_form_edit = $('#dialog_form_edit');
    $dialog_form_add = $('#dialog_form_add');
    $dialog_confrim_delete = $('#dialog_confrim_delete');
    
    oTable = $datatable.dataTable({
        "bJQueryUI": true,
        "sPaginationType": "full_numbers",
        "bInfo": false,
        "bProcessing": true,
        "bServerSide": true,
        "bPaginate": false,
        "bFilter" : false,
        "bLengthChange": false,
        //"bFilter": false,
        "sAjaxSource": $datatable.attr('active_url'),
        "fnDrawCallback": function( oSettings ) {
            //$("#datatable_nb_wrapper").find(".fg-toolbar").removeClass("ui-widget-header");
            datatable_selected();
            _DATATABLE_TR = null;
        },
        "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
            if( aData['id'] != undefined ){
                $(nRow).attr("value_id",aData['id']);
            }
            if( aData['modify'] != undefined ){
                $(nRow).attr("modify",aData['modify']);
            }
            //if ( aData[4] == "A" ){$('td:eq(4)', nRow).html( '<b>A</b>' );}
            return nRow;
        }
        //"oTableTools": {"aButtons": ["copy", "csv", "xls", "pdf"]}
        
    });
    dialogFormAddNew();
    dialogConfrimDelete();
    dialogFormEdit();
    dialogError();
} );

function datatable_selected(){
    $datatable.find('tbody tr[modify=edit]').click( function( e ) {
        if ( $(this).hasClass('row_selected') ) {
            $(this).removeClass('row_selected');
            _DATATABLE_TR = null;
        }else {
            oTable.$('tr.row_selected').removeClass('row_selected');
            $(this).addClass('row_selected');
            _DATATABLE_TR = $(this).attr('value_id');
        }
    });
    
    $datatable.find('tbody tr[modify=edit]').bind("dblclick", function(){
        if ( !$(this).hasClass('row_selected') ) {
            $(this).addClass('row_selected');
        }
        _DATATABLE_TR = $(this).attr('value_id');
        even_edit_dbclick( _DATATABLE_TR );

    });

}

function even_edit_dbclick(id){
    var url = $dialog_form_edit.attr('active_url') + '/' + id;
        
    $.get(url, function(data) {
        $dialog_form_edit.html( data );
        $dialog_form_edit.dialog( "open" );
    });
}

function dialogFormAddNew(){
    
    $dialog_form_add.dialog({
        autoOpen: false,
        //height: 300,
        width: 320,
        modal: true,
        resizable: true,
        buttons: {
            "<?php print lang('btn_save') ?>": function() {
                var $this = $( this );
                $("#frm_add").ajaxSubmit({
                    //beforeSubmit: validate
                    dataType:  'json',
                    beforeSubmit: function(formData, jqForm, options) {
                        return validate_frm();
                    },
                    success: function(data) {
                        if(data.action == 'insert'){
                            //oTable.fnDraw();
                            //oTable.fnFilter( data.ten );
                            oTable.fnSort( [ [0,'desc'] ] );
                        }
                        $this.dialog( "close" );
                    }
                });
            },
            "<?php print lang('btn_cancel') ?>": function() {
                $( this ).dialog( "close" );
            }
        },
        close: function() {
            $dialog_form_add.html('');
        }
    });
        
}

function dialogFormEdit(){
    
    $dialog_form_edit.dialog({
        autoOpen: false,
        //height: 300,
        width: 320,
        modal: true,
        resizable: true,
        buttons: {
            "<?php print lang('btn_save') ?>": function() {
                var $this = $( this );
                $("#frm_add").ajaxSubmit({
                    //beforeSubmit: validate
                    dataType:  'json',
                    beforeSubmit: function(formData, jqForm, options) {
                        return validate_frm();
                    },
                    success: function(data) {
                        if(data.action == 'update'){
                            var $tr = oTable.$('tr[value_id="'+ data.id +'"]');
                            $tr.find('td:eq(1)').html(data.name);
                            $tr.find('td:eq(2)').html(data.status);
                        }
                        $this.dialog( "close" );
                    }
                });
            },
            "<?php print lang('btn_cancel') ?>": function() {
                $( this ).dialog( "close" );
            }
        },
        close: function() {
            $dialog_form_edit.html('');
        }
    });
        
}

function validate_frm(){
    var flag = true;
    var name = $('input[name=name]').val();
    if(name == ''){
        $("input[name=name]").addClass("i-error");
        $("#iname").show();
        flag = false;
    }else{
        $("#iname").hide();
        $("input[name=name]").removeClass("i-error");
    }
    
    return flag;
}

function dialogConfrimDelete(){
    
    $dialog_confrim_delete.dialog({
        autoOpen: false,
        //height: 300,
        width: 320,
        modal: true,
        resizable: true,
        buttons: {
            "Delete": function() {
                var url = $dialog_confrim_delete.attr('active_url');
                
                $.post(url, { id: _DATATABLE_TR },function(data){
                     _DATATABLE_TR = null;
                     oTable.fnDraw();
                }, "json");
                
                $( this ).dialog( "close" );
            },
            Cancel: function() {
                $( this ).dialog( "close" );
            }
        },
        close: function() {
            //allFields.val( "" ).removeClass( "ui-state-error" );
        }
    });
        
}

function even_add(){
    var url = $dialog_form_add.attr('active_url');
    $.get(url, function(data) {
        $dialog_form_add.html( data );
        $dialog_form_add.dialog( "open" );
    });
}

function even_edit(){
    if(_DATATABLE_TR != null){
        var url = $dialog_form_edit.attr('active_url') + '/' + _DATATABLE_TR;
        $.get(url, function(data) {
            $dialog_form_edit.html( data );
            $dialog_form_edit.dialog( "open" );
        });
    }else{
        $dialog_error.html( '<?php print lang('error_noselected'); ?>' );
        $dialog_error.dialog( "open" );
    }
}

function even_delete(){
    if(_DATATABLE_TR != null){
        $dialog_confrim_delete.dialog( "open" );
    }else{
        $dialog_error.html( '<?php print lang('error_noselected'); ?>' );
        $dialog_error.dialog( "open" );
    }
}
</script>

<div class="menu-button">
    <div class="message"></div>
    <!--<button onclick="even_add();"><?php print lang('btn_new'); ?></button>
    <button onclick="even_edit();"><?php print lang('btn_edit'); ?></button>
    <button onclick="even_delete();"><?php print lang('btn_delete'); ?></button>-->
    <div class="clear"></div>
</div>

<div class="warpp">
    <div>
        <table>
            <tbody>
                <tr>
                    <td>
                        <table>
                            <tbody>
                                <tr>
                                    <td><label><?php print lang('vt_nguoinhap'); ?>:</label></td>
                                    <td><?php print $bill->username; ?></td>
                                </tr>
                                <tr>
                                    <td><label><?php print lang('vt_maphieunhap'); ?>:</label></td>
                                    <td><input name="bill_code" value="<?php print $bill->bill_code; ?>" class="itext ivalidate ui-widget-content ui-corner-all"></td>
                                </tr>
                                <tr>
                                    <td><label><?php print lang('vt_nhacungcap'); ?>:</label></td>
                                    <td>
                                        <select name="supplier_id" class="itext ivalidate ui-widget-content ui-corner-all">
                                            <option><?php print $bill->supplier_name; ?></option>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td><label><?php print lang('vt_total-vat'); ?>:</label></td>
                                    <td><input type="text" name="bill_total" id="bill_total" value="<?php print decimal_format($bill->bill_total); ?>" class="itext ivalidate ui-widget-content ui-corner-all"></td>
                                </tr>
                                <tr>
                                    <td><label><?php print lang('vt_vat%'); ?>:</label></td>
                                    <td><input name="bill_vat" id="bill_vat" value="<?php print decimal_format($bill->bill_vat); ?>" class="itext ivalidate ui-widget-content ui-corner-all"></td>
                                </tr>
                                <tr>
                                    <td><label><?php print lang('vt_vatprice'); ?>:</label></td>
                                    <td><input name="bill_vatprice" id="bill_vatprice" value="<?php print decimal_format($bill->bill_vatprice); ?>" class="itext ivalidate ui-widget-content ui-corner-all"></td>
                                </tr>
                            </tbody>
                        </table>
                    </td>
                    
                    <td>
                        <table>
                            <tr>
                                <td><label><?php print lang('vt_note'); ?>:</label></td>
                            </tr>
                            <tr>
                                <td>
                                    <textarea name="bill_note" style="width: 380px;" cols="40" rows="3"><?php print $bill->bill_note; ?></textarea>
                                </td>
                            </tr>
                            <tr>
                                <td><label><?php print lang('vt_total+vat'); ?>:</label> <input type="text" name="bill_totalvat" id="bill_totalvat" value="<?php print decimal_format($bill->bill_totalvat); ?>" class="itext ivalidate ui-widget-content ui-corner-all"></td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
    <table cellpadding="0" cellspacing="0" border="0" class="display" id="datatable_dvt" active_url="<?php print site_url('nk/datatable_bill/'.$bill->id); ?>">
        <thead>
            <tr>
                <th width="40px"><?php print lang('vt_stt'); ?></th>
                <th width="100px"><?php print lang('vt_mavt'); ?></th>
                <th width="200px"><?php print lang('vt_tenvt'); ?></th>
                <th><?php print lang('vt_donvitinh'); ?></th>
                <th><?php print lang('vt_soluongnhap'); ?></th>
                <th><?php print lang('vt_tenphanloai'); ?></th>
                <th><?php print lang('vt_note'); ?></th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td colspan="7" class="dataTables_empty">Loading data from server</td>
            </tr>
        </tbody>
    </table>
</div>

<div id="dialog_form_add" title="<?php print lang('vt_title_themdvt'); ?>" active_url="<?php print site_url('vt/ajax_loadfrm_dvt/'); ?>"></div>
<div id="dialog_form_edit" title="<?php print lang('vt_title_suadvt'); ?>" active_url="<?php print site_url('vt/ajax_loadfrm_dvt/'); ?>"></div>
<div id="dialog_confrim_delete" title="<?php print lang('title_thongbao'); ?>" active_url="<?php print site_url('vt/delete_dvt/'); ?>">
    <p><?php print lang('msg_delete'); ?></p>
</div>